let query = {
  start_time:'',
  end_time:'',
  title:'',
  cateid:'',
  page:1,
  limit:10,
}

layui.use(['laydate','table'],function(){
  var layer = layui.layer;
  var laydate = layui.laydate;
  var form = layui.form;
  var $ = layui.$;
  var table = layui.table;
  var laypage = layui.laypage;

  laydate.render({
    elem: '#date',
    range: '至',
  });
    // 渲染
function render(){
  axios.get('ydd_article/list',{params:query})
.then(({data:{data}}) => {
  // console.log(data)
  renderPage(data.count)
  data.data.map(item=>{
    item.create_date = new Date(item.create_date).toLocaleDateString()
    item.status = item.status === 1 ? '已发布' : '待审核'
    item.pic = `http://124.223.14.236:8060/${item.pic}`
    return item
  })

    //表格渲染
  table.render({
    elem: '#table-lj',
   data:data.data, // 此处为静态模拟数据，实际使用时需换成真实接口
    defaultToolbar: '',
    toolbar: '#toolBarDemo1',
    cols: [function(){
      var arr = [
        {field:'id', title:'ID', },
        {field:'title', title:'标题', },
        {field:'catename', title:'分类名', },
        {field:'pic', title:'图片',toolbar:'#imgTpl' },
        {field:'create_date', title:'创建时间', },
        {field:'ishot', title:'是否推荐',toolbar:'#ID-table-demo-templet-switch1' },
        {field:'istop', title:'是否置顶',toolbar:'#ID-table-demo-templet-switch2' } ,
        {field:'status', title:'文章状态', toolbar:'#txtTpl'} ,
        { title:'操作',toolbar: `#toolBarDemo2`, width:137} ,
      ];
      
      // 初始化筛选状态
      var local = layui.data('table-filter-test'); // 获取对应的本地记录
      layui.each(arr, function(index, item){
        if(item.field in local){
          item.hide = local[item.field];
        }
      });
      return arr;
    }() ],  
    initSort: {
      // 渲染表头单元格时设置背景色
      templet: function(field) {
        return '<div style="background-color: red;">' + field.title + '</div>';
      }
    },
    done: function(){
      // 记录筛选状态
      var that = this;
      that.elem.next().on('mousedown', 'input[lay-filter="LAY_TABLE_TOOL_COLS"]+', function(){
        var input = $(this).prev()[0];
        // 此处表名可任意定义
        layui.data('table-filter-test', {
          key: input.name
          ,value: input.checked
        })
      });
    }
    
  });

  //删除功能  
$('.layui-table-box').on('click','#delbtn',function(){
  axios.get('ydd_article/del',{params:{id:$(this).attr('data-id')}})
  .then((res) => {
    // console.log(res.data);
    $(this).parents('tr').remove()
    layer.msg('删除成功')
    render()
  })
  })
  //编辑
$('.layui-table-box').on('click','#editbtn',function(){
  const id = $(this).attr('data-id')
  // console.log(id);
  location.href='./editarticle.html?id='+id
 

})
//新增
$('#addItem').on('click',function(){
window.parent.$('#addar').click()
 location.href = 'addarticle.html'
})
  let flag = true;
    $('#formChange').on('click',function(){
    if(flag){
      $('#form-lj').hide();
      $('#formChange i').css('transform', 'rotate(180deg)')
      flag = false;
    }else{
      $('#form-lj').show();
      $('#formChange i').css('transform', 'rotate(0deg)')
      flag = true;
    }
    })
    $('#refresh').on('click',function(){
         location.reload()
      // table.reload('table-lj', {
      //   where: { // 传递数据异步请求时携带的字段
      //    data: data.data
      //   }
        
      // })
    }) 
})
}
render()
function renderPage(count){
  laypage.render({
    elem: 'page-box',
    theme: '#1E9FFF',
    count,
    limits:[10,20,40,50,100],
    curr:query.page,
    limit:query.limit,
    groups:5,
    prev:'<i class="fa fa-angle-left"></i>',
    next:'<i class="fa fa-angle-right"></i>',
    layout:['count','limit','prev', 'page', 'next','skip'],
    jump: function(obj, first){
      //obj包含了当前分页的所有参数，比如：
     // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
      //console.log(obj.limit); //得到每页显示的条数
      query.page=obj.curr
        query.limit=obj.limit
      //首次不执行
      if(!first){
        //do something
        render()
      }
    }
  });
}
//分类渲染
axios.get('ydd_cate/all').then(({data:{data}})=>{
  // console.log(data);
 const catearr= data.map(({id,catename})=>{
    return ` <option value="${id}">${catename}</option>`
  }).join('')
  $('#catearr').append(catearr)
  form.render('select');
})
//搜索
$('#search').on('click',function(e){
  e.preventDefault()
query.title = $('[name="title"]').val()
query.cateid = $('[name="cateid"]').val()
query.start_time = Date.parse($('#date').val().substring(0,10))
query.end_time = Date.parse($('#date').val().substring(13))
isNaN(query.start_time)?query.start_time = '':query.start_time
isNaN(query.end_time)?query.end_time = '':query.end_time
// console.log(query.start_time,query.end_time);
// console.log(query);
render()

  })
  //重置
  $('#reset').on('click',function(){
    location.reload()
})

});

